<?php
class Data_Base
{
    protected static $_all_items;

    public static function counts ($table, $where = NULL)
    {
        $query = DB::select(array('count("*")', 'count'))
            ->from($table);

        if (! empty($where))
        {
            if (is_array($where) && is_array($where[0]))
            {
                foreach ($where as $rs)
                    $query->where($rs[0], $rs[1], $rs[2]);
            }
            else
            {
                $query->where($where[0], $where[1], $where[2]);
            }
        }

        return (int) $query->execute()->get('count');
    }

    public static function lists ($table, $where, $sort, $offset, $limit, $selects = NULL)
    {
        if (empty($selects))
            $query = DB::select()->from($table);
        else
            $query = DB::select($selects)->from($table);

        if (! empty($where))
        {
            if (is_array($where) && is_array($where[0]))
            {
                foreach ($where as $rs)
                    $query->where($rs[0], $rs[1], $rs[2]);
            }
            else
            {
                $query->where($where[0], $where[1], $where[2]);
            }
        }

        $query->order_by($sort[0], $sort[1]);
        $query->offset($offset)->limit($limit);

        return $query->execute()->as_array();


    }

    public static function item ($table, $primary_key, $nid)
    {
        $query = DB::select()
            ->from($table)
            ->where($primary_key, '=', $nid)
            ->limit(1)
            ->execute();

        if ($query->count() == 0)
        {
            return array();
        }
        else
        {
            return $query->current();
        }
    }

    protected static function load_all($tableName, $primaryKey = 'id')
    {
        $query = DB::select()
            ->from($tableName)
            ->execute();

        $ret = array();
        foreach ($query as $rs)
        {
            $id = $rs[$primaryKey];
            $ret[$id] = $rs;
        }

        return $ret;
    }

    protected static function imageSrc ($src_mixed_string)
    {
        $arrSrcs = explode('|', $src_mixed_string);
        foreach ($arrSrcs as $src)
        {
            if (! empty($src))
                return $src;
        }

        return '';
    }
    
    public function getColumnValue ($nid, $columnKey)
    {
        $v = DB::select($columnKey)
            ->from($this->_commTableName)
            ->where($this->_commPrimaryKey, '=', $nid)
            ->limit(1)
            ->execute()
            ->get($columnKey);

        return $v;
    }
    
    public function incColumnValue ($nid, $type, $n)
    {
        if ($n > 0)
        {
            DB::update($this->_commTableName)
            ->set(array($type => DB::expr("`$type` + $n")))
            ->where($this->_commPrimaryKey, '=', $nid)
            ->execute();
        }
        else
        {
            $n = abs($n);
            DB::update($this->_commTableName)
            ->set(array($type => DB::expr("`$type` - $n")))
            ->where($this->_commPrimaryKey, '=', $nid)
            ->execute();
        }
    }
}
